/**
 * 多窗口ACTION
 */
package cn.antia.cwin.page;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.*;

import cn.antia.cep.common.*;
import cn.antia.cep.common.pool.AntiaPoolHelper;
import cn.antia.cwin.page.StrutsAction;
import cn.antia.cep.page.action.ActionException;
import cn.antia.cwin.window.*;
import cn.antia.cwin.Cele;

/**
 * @author sunkeei
 * @date Dec 29, 2007
 * @time 4:21:31 PM
 */
public class MultiWindowAction extends StrutsAction {
	private MultiWindow multiWindow;
	
	private AntiaPoolHelper antiaPoolHelper;
	public AntiaPoolHelper getAntiaPoolHelper() {
		return antiaPoolHelper;
	}
	public void setAntiaPoolHelper(AntiaPoolHelper antiaPoolHelper) {
		this.antiaPoolHelper = antiaPoolHelper;
	}

	protected String action() throws AntiaException {
		multiWindow = null;
		String id = request.getParameter("id");
		if(id == null || id.trim().length() < 1){
			request.setAttribute("message", "没有传入正确的参数。");
			return "error";
		}
		boolean isMulti = false;					//是否允许同一页面存在两个相同ID的window
		if(id.trim().length() < 1){
			ActionException ae = new ActionException("012","Not get a window ID");
			SysExceptionTool.addExceptionInfo(ae);
			throw ae;
		}
		multiWindow = antiaPoolHelper.getMultiWindow(id);
		if(multiWindow == null){
			ActionException ae = new ActionException("012","Not found a multi window by ID=" + id);
			SysExceptionTool.addExceptionInfo(ae);
			throw ae;
		}
		/*
		Map map = request.getParameterMap();
        Iterator pamas = map.keySet().iterator();
        String parm;
        while(pamas.hasNext()){
        	parm = (String)pamas.next();
        	//是否可以在同一页面出现多个相同ID的WINDOW
        	if(parm.equals("multi")){
        		isMulti = true;
        		continue;
        	}
        }*/
        if(!isMulti){
        	//通知页面，当前的windowId
        	request.setAttribute(WindowConst.JWAS_MULTI_WINDOW_ID,id);
        	session.setAttribute(WindowConst.JWAS_MULTI_WINDOW_ID,multiWindow);
        	init(request,multiWindow.getPrimary());
        }else{
        	throw new AntiaException("000","方法未实现,multiWindowAction:multi");
        }
        if(multiWindow.getType().equals("onemany")){
        	return "pri";
        }else if(multiWindow.getType().equals("oneone"))
        	return "oneone";
		return null;
	}
	
	/**
	 * 将Window信息装入session
	 * @param request request
     * @param id id
	 */
	protected void init(HttpServletRequest request,String id){
		boolean isMulti = false;					//是否允许同一页面存在两个相同ID的window
		if(id == null || id.trim().length() < 1){
			ActionException ae = new ActionException("012","Not get a window ID");
			SysExceptionTool.addExceptionInfo(ae);
			throw ae;
		}
		SingleWindow window = antiaPoolHelper.getSingleWindow(id);
		if(window == null){
			ActionException ae = new ActionException("012","Not found a window by ID=" + id);
			SysExceptionTool.addExceptionInfo(ae);
			throw ae;
		}
		Map map = request.getParameterMap();
        Iterator pamas = map.keySet().iterator();
        String parm;
        Cele ec,ecadd;
        while(pamas.hasNext()){
        	parm = (String)pamas.next();
        	//是否可以在同一页面出现多个相同ID的WINDOW
        	if(parm.equals("multi")){
        		isMulti = true;
        		continue;
        	}
        	//如果指定action
        	if(parm.equals("was")){
        		window.setAction(request.getParameter(parm));
        		continue;
        	}
        	if(parm.equals("id")){
        		continue;
        	}
        	//查看是否有这个字段
        	if(window.hasField(parm)){
                ec = new Cele();
        		ec.getHtmlWidget().setAttr("readonly","true");
        	}
            /*
            附加字段另作处理 --- 注释在 080722
            else{
             
        		//如果没有，则查看是否在附加字段中已有定义
        		ecadd = Window.existField(window.getAdditive(), parm);
        		//如果在附加字段中有定义，那么覆盖之前定义
        		if(ecadd != null){
        			ecadd.setValue(request.getParameter(parm));
        			ecadd.getHtmlWidget().setAttr("readonly","true");
        		}else{
        			//如果在附加字段中没有定义，则加入到附加字段中
        			ecadd = new Cele();
        			ecadd.getHtmlWidget().setHtmlType("hidden");
        			ecadd.getField().setFieldName(parm);
        			ecadd.setValue(request.getParameter(parm));
        			ecadd.getHtmlWidget().setAttr("readonly","true");
        			List list = window.getAdditive();
        			if(list == null) list = new ArrayList();
        			list.add(ecadd);
        			window.setAdditive(list);
        		}
        	}*/
        }
        window.setAction(multiWindow.getPrimaryWas());
        if(!isMulti){
        	//通知页面，当前的windowId
        	request.setAttribute(WindowConst.JWAS_SINGLE_WINDOW_ID,id);
        	session.setAttribute(id,window);
        }else{
        	throw new AntiaException("000","方法未实现,windowAction:multi");
        }
        
	}
}
